package org.linloong.modules.apis.monitor.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.linloong.modules.apis.monitor.entity.*;
import org.linloong.modules.apis.monitor.service.RealTimeDataService;
import org.linloong.modules.apis.monitor.sync.DailyDataSyncService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("/api/monitor")
@RequiredArgsConstructor
@Tag(name = "监测平台接口", description = "基于《监测平台WebAPI接口规范v2.docx》实现的对外接口，包含实时数据查询")
public class MonitorController {
    private final RealTimeDataService realTimeDataService;
    private final DailyDataSyncService dailyDataSyncService;

    // -------------------------- 设备信息接口（文档"气象设备接口"章节） --------------------------
    @Operation(summary = "获取设备信息", description = "对应文档\"气象设备接口\"：GET /api/Deviceinfoes('{deviceId}')")
    @GetMapping("/device/info/{deviceId}")
    public Mono<DeviceInfo> getDeviceInfo(
            @Parameter(description = "设备ID（如ZW101579，文档中\"设备号/设备ID\"）", required = true, example = "ZW101569")
            @PathVariable String deviceId) {
        return realTimeDataService.getDeviceInfo(deviceId);
    }

    // -------------------------- 气象数据接口（文档"气象数据接口"章节） --------------------------
    @Operation(summary = "获取气象设备最新数据", description = "对应文档\"气象数据接口\"：GET /api/devicedatas('{deviceId}')/Current")
    @GetMapping("/weather/latest/{deviceId}")
    public Mono<WeatherData> getLatestWeatherData(
            @Parameter(description = "设备ID", required = true, example = "ZW000001")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestWeatherData(deviceId);
    }

    @Operation(summary = "获取气象设备时间范围数据", description = "对应文档\"气象数据接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/weather/range/{deviceId}")
    public Mono<ApiResponse<WeatherData>> getWeatherDataByTimeRange(
            @Parameter(description = "设备ID", required = true, example = "ZW000001")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getWeatherDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 物候接口（文档"物候接口"章节） --------------------------
    @Operation(summary = "获取物候设备最新数据", description = "对应文档\"物候接口\"：GET /api/PhenologyData('{deviceId}')/Current()")
    @GetMapping("/phenology/latest/{deviceId}")
    public Mono<PhenologyData> getLatestPhenologyData(
            @Parameter(description = "设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestPhenologyData(deviceId);
    }

    @Operation(summary = "获取物候设备时间范围数据", description = "对应文档\"物候接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/phenology/range/{deviceId}")
    public Mono<ApiResponse<PhenologyData>> getPhenologyDataByTimeRange(
            @Parameter(description = "设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getPhenologyDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 盖度接口（文档"盖度接口"章节） --------------------------
    @Operation(summary = "获取盖度设备最新数据", description = "对应文档\"盖度接口\"：GET /api/CoverageData('{deviceId}')/Current()")
    @GetMapping("/coverage/latest/{deviceId}")
    public Mono<CoverageData> getLatestCoverageData(
            @Parameter(description = "设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestCoverageData(deviceId);
    }

    @Operation(summary = "获取盖度设备时间范围数据", description = "对应文档\"盖度接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/coverage/range/{deviceId}")
    public Mono<ApiResponse<CoverageData>> getCoverageDataByTimeRange(
            @Parameter(description = "设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getCoverageDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 盖度+接口（文档"盖度+接口"章节，2.7版本新增） --------------------------
    @Operation(summary = "获取盖度+设备最新数据", description = "对应文档\"盖度+接口\"：GET /api/CoveragePlusData('{deviceId}')/Current()")
    @GetMapping("/coverage-plus/latest/{deviceId}")
    public Mono<CoveragePlusData> getLatestCoveragePlusData(
            @Parameter(description = "盖度+设备ID（如ZW202406）", required = true, example = "ZW202406")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestCoveragePlusData(deviceId);
    }

    @Operation(summary = "获取盖度+设备时间范围数据", description = "对应文档\"盖度+接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/coverage-plus/range/{deviceId}")
    public Mono<ApiResponse<CoveragePlusData>> getCoveragePlusDataByTimeRange(
            @Parameter(description = "盖度+设备ID", required = true, example = "ZW202406")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getCoveragePlusDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 光谱物候接口（文档"光谱物候接口"章节，2.3版本新增） --------------------------
    @Operation(summary = "获取光谱物候设备最新数据", description = "对应文档\"光谱物候接口\"：GET /api/SpectrumData('{deviceId}')/Current()")
    @GetMapping("/spectrum/latest/{deviceId}")
    public Mono<SpectrumData> getLatestSpectrumData(
            @Parameter(description = "光谱设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestSpectrumData(deviceId);
    }

    @Operation(summary = "获取光谱物候设备时间范围数据", description = "对应文档\"光谱物候接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/spectrum/range/{deviceId}")
    public Mono<ApiResponse<SpectrumData>> getSpectrumDataByTimeRange(
            @Parameter(description = "光谱设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getSpectrumDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 物候+接口（文档"物候+接口"章节，2.5版本新增） --------------------------
    @Operation(summary = "获取物候+设备最新数据", description = "对应文档\"物候+接口\"：GET /api/PhenologyPlusData('{deviceId}')/Current()")
    @GetMapping("/phenology-plus/latest/{deviceId}")
    public Mono<PhenologyPlusData> getLatestPhenologyPlusData(
            @Parameter(description = "物候+设备ID（如ZW202311）", required = true, example = "ZW202311")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestPhenologyPlusData(deviceId);
    }

    @Operation(summary = "获取物候+设备时间范围数据", description = "对应文档\"物候+接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/phenology-plus/range/{deviceId}")
    public Mono<ApiResponse<PhenologyPlusData>> getPhenologyPlusDataByTimeRange(
            @Parameter(description = "物候+设备ID", required = true, example = "ZW202311")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getPhenologyPlusDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 虫情灯接口（文档"虫情灯数据接口"章节） --------------------------
    @Operation(summary = "获取虫情灯设备最新数据", description = "对应文档\"虫情灯数据接口\"：GET /api/InsectData('{deviceId}')/Current()")
    @GetMapping("/insect/latest/{deviceId}")
    public Mono<InsectData> getLatestInsectData(
            @Parameter(description = "虫情灯设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestInsectData(deviceId);
    }

    @Operation(summary = "获取虫情灯设备时间范围数据", description = "对应文档\"虫情灯数据接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/insect/range/{deviceId}")
    public Mono<ApiResponse<InsectData>> getInsectDataByTimeRange(
            @Parameter(description = "虫情灯设备ID", required = true, example = "ZW101569")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getInsectDataByTimeRange(deviceId, startTs, endTs);
    }

    // -------------------------- 声纹接口（文档"声纹数据接口"章节，2.6版本新增） --------------------------
    @Operation(summary = "获取声纹设备最新数据", description = "对应文档\"声纹数据接口\"：GET /api/AudioPatternData('{deviceId}')/Current()")
    @GetMapping("/audio-pattern/latest/{deviceId}")
    public Mono<AudioPatternData> getLatestAudioPatternData(
            @Parameter(description = "声纹设备ID（如ZW202404）", required = true, example = "ZW202404")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestAudioPatternData(deviceId);
    }

    @Operation(summary = "获取声纹设备时间范围数据", description = "对应文档\"声纹数据接口\"：带$filter的GET请求，时间戳为Unix毫秒级")
    @GetMapping("/audio-pattern/range/{deviceId}")
    public Mono<ApiResponse<AudioPatternData>> getAudioPatternDataByTimeRange(
            @Parameter(description = "声纹设备ID", required = true, example = "ZW202404")
            @PathVariable String deviceId,
            @Parameter(description = "开始时间戳（Unix毫秒）", required = true, example = "1592271960000")
            @RequestParam Long startTs,
            @Parameter(description = "结束时间戳（Unix毫秒）", required = true, example = "1592358360000")
            @RequestParam Long endTs) {
        return realTimeDataService.getAudioPatternDataByTimeRange(deviceId, startTs, endTs);
    }
    // -------------------------- 新增：解析后的气象数据接口 --------------------------
    @Operation(summary = "获取气象设备最新解析数据", description = "dataArray与layout匹配后的结果，含计算后的值和单位")
    @GetMapping("/weather/parsed/latest/{deviceId}")
    public Mono<List<WeatherDataParsed>> getLatestWeatherDataParsed(
            @Parameter(description = "设备ID", required = true, example = "ZW000001")
            @PathVariable String deviceId) {
        return realTimeDataService.getLatestWeatherDataParsed(deviceId);
    }

    // -------------------------- 新增：测试定时任务 --------------------------
    @GetMapping("/weather/sync/test")
    public void getLatestWeatherDataParsed() {
//         dailyDataSyncService.calculateYesterdayDailyAvg();
    }
}